<?php

function & D($name){
    global $lang;
    static $models=array();
    $name=ccase(trim($name));
    $mName=md5(APP.MOD.ACT.$name);
    if (isset($models[$mName])){
        return $models[$mName];
    }else {
        if (file_exists(model_path.'/'.$name.'.model.php')){
            require_once APP_LIB.'model.php';
            require_once model_path.'/'.$name.'.model.php';
            $name=ucfirst(ccase($name,false)).'Model';
            $models[$mName]=new $name();
            return $models[$mName];
        }else {
            throw new Exception($lang['NOT_MODEL']);
        }
            
    }
}

//驼峰命名转换
function ccase($name,$underLine=true){
    if ($underLine){
      $name=preg_replace("/([A-Z]+)/", '_\\1', $name);
      $name=strtolower(ltrim($name,'_'));
    }else{
      $name=preg_replace("/_([a-z])/eis", "strtoupper('\\1')", $name);
    }
    
    return $name;
}


function & M(){
    include APP_LIB.'model.php';
    $obj= new Model;
    return $obj;
}


function exception_handler($excObj){
    global $cfg;
    header('Content-Type:text/html;charset='.$cfg['charset']);
    echo 'Error:'.$excObj->getMessage()."\n";
}



function photopath($path=''){
    return '../commanage/'.$path;//'../commanage/'.$path

}


function formatNumber($num,$intFlag=1){
    if(is_numeric($num)){
         settype($num,'string');
         $num=explode('.',$num);
         return $intFlag?$num[0]:(empty($num[1])?'.00':'.'.$num[1]);
    }
}


//加一
function add($num){
    return ++$num;
}

function sttodate($st,$flag=false){
    return $flag?rawurlencode(date('Y-m-d    H:i:s',$st)):date('Y-m-d    H:i:s',$st);
}
function sttosdate($st){
    return date('Y-m-d',$st);
}
function numtostr($num){
    $leader=array('1'=>'华敏','2'=>'李辉川','3'=>'沈立昀','4'=>'邹师','5'=>'袁永高','6'=>'黄春刚');
    return $leader[$num];
}

function numtoword($num){
    return $num?'有':'没有';
}
//cache

function & cache_server(){
    static $CS=NULL;
    if ($CS===null){
        switch (CACHE_SERVER){
            case 'memcached':
            import('cache');
            list($host,$port)=explode(':', CACHE_MEMCACHED);
            $CS=new MemcacheServer(array(
                'host'=>$host,
                'port'=>$port,
            ));
            break;
        }
    }
    return $CS;
}
